package Part_3;

public class Test {
    public static void main(String[] args) {
        StringUtil su=new StringUtil();
        su.deal("ABC");
        su.deal("ABC456");
        su.deal("ABC456cd6f");
        su.deal("ABC456cde76ed");
        su.deal("456ABC456cde76ed");
        su.deal("456ABC");
    }
}

class StringUtil{
    /*
     * 将StringBuffer当作堆栈来处理
     * 创建最终输出的字符串存放StringBuffer sb
     * 创建临时存放数字StringBuffer sb2
     * 将接收的待处理字符串str转成字符数组
     * 遍历字符数组，判断是否为数字，是的话压入sb2，否的话，先判断sb2是否为空，非空就先出栈再压入sb，出栈后清空sb2，再把当前循环的字符压入sb
     * 最终判断sb2是否为空，非空就出栈再压入sb，出栈后清空sb2
     * 最终输出sb
     * */
    void deal(String str){
        StringBuffer sb=new StringBuffer();
        StringBuffer sb2=new StringBuffer();
        char charArr [] = str.toCharArray();
        for (int i = charArr.length-1; i >= 0; i--) {
            if (Character.isDigit(charArr[i])){
                sb2.append(charArr[i]);
            }else{
                if(sb2.length()>0){
                    for (int j = sb2.length()-1; j >= 0; j--) {
                        sb.append(sb2.charAt(j));
                    }
                    sb2.delete(0,sb2.length());
                }
                sb.append(charArr[i]);
            }
        }
        if(sb2.length()>0){
            for (int j = sb2.length()-1; j >= 0; j--) {
                sb.append(sb2.charAt(j));
            }
            sb2.delete(0,sb2.length());
        }
        System.out.println(sb);

    }
}
